package org.ar.tiendaonline.persistence;

import java.util.Iterator;
import java.util.List;

import org.ar.tiendaonline.vo.Factura;
import org.ar.tiendaonline.vo.Proveedor;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ProveedorHibernateDao implements ProveedorDao {

	private List<Proveedor> provList;
	private Proveedor prov;
	private Factura fac;
	
	private List<Factura> facList;

	public List getAllProveedores() {
		Session session = HibernateUtil.getSession();

		try {
			session.beginTransaction();
			provList = session.createQuery("from Proveedor p order by p.nombre asc").list();
			return provList;
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		}

	}

	public Proveedor getProveedor(Integer id) {
		Session session = HibernateUtil.getSession();

		try {
			session.beginTransaction();
			Query q = session.createQuery("from Proveedor as p where p.proveedorId =:proveedorId");
			q.setInteger("proveedorId", id);
			return (Proveedor) q.uniqueResult();
		} finally {
			session.close();
		}
	}

	public void insertProveedor(Proveedor prov) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;

		try {
			tx = session.beginTransaction();
			session.save(prov);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void updateProveedor(Proveedor prov) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(prov);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	public void deleteProveedor(Integer id) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			prov = (Proveedor) session.get(Proveedor.class, id);
			session.delete(prov);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}
	
	public List showFacturasProv(Integer id) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			prov = (Proveedor) session.get(Proveedor.class, id);
			facList = (List)session.createQuery("from Factura as f where id_proveedor = " + prov.getProveedorId()).list();
			return facList;
		} catch (HibernateException e) {
			throw e;
		} finally {
			session.close();
		
		}
		
	}
	
//	public void insertProvFactura(Factura fac) {
//		Session session = HibernateUtil.getSession();
//		Transaction tx = null;
//			try {
//				tx = session.beginTransaction();
//				fac = (Factura) session.get(Factura.class, fac.getFacturaId());
//				prov = (Proveedor) session.get(Proveedor.class, prov.getProveedorId());
//				prov.agregarFactura(fac);
//				session.save(prov);
//				tx.commit();
//			
//			} catch (RuntimeException e) {
//				if (tx != null)
//					tx.rollback();
//				throw e;
//			} finally {
//				session.close();
//		}
//	}
	


	public static void main(String[] args) {
		ProveedorHibernateDao provDao = new ProveedorHibernateDao();
		List<Proveedor> provs = provDao.getAllProveedores();
		for (Iterator<Proveedor> i = provs.iterator(); i.hasNext();) {
			System.out.println(i.next().getNombre());

		}
	}



}
